package net.m510.autosurfer;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import net.m510.autosurfer.common.cli.OutFileOption;
import net.m510.autosurfer.core.AbstractMain;
import net.m510.autosurfer.core.UserAgents;
import net.m510.autosurfer.core.exception.UserProcessException;
import net.m510.autosurfer.smbc.SmbcDownload;
import net.m510.autosurfer.smbc.SmbcGoDetail;
import net.m510.autosurfer.smbc.SmbcGoTop;
import net.m510.autosurfer.smbc.SmbcLogin;
import net.m510.autosurfer.smbc.SmbcLoginChecker;
import net.m510.autosurfer.smbc.SmbcLogout;
import net.m510.autosurfer.smbc.SmbcTopChecker;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.nodes.Document;

public class SMBCAccountGetMain extends AbstractMain {
	private static final Logger log = Logger.getLogger(SMBCAccountGetMain.class.getName());
	private static final String DEFAULT_MEISAI_FILENAME="meisai.csv";
	private static final String DEFAULT_USER_AGENT = UserAgents.CHROME;
	
	private String userName1;
	private String userName2;
	private String password;
	private String accountNum;
	private File outFile;

	/**
	 * 
	 */
	public SMBCAccountGetMain() {
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SMBCAccountGetMain main = new SMBCAccountGetMain();
		main.setLogger(log);
		main.Main(args);
	}
	
	@Override
	protected void customClientBuilder(HttpClientBuilder builder) {
		builder.setUserAgent(DEFAULT_USER_AGENT);
	}

	@Override
	protected Document start() throws IOException,UserProcessException {
		SmbcGoTop p = new SmbcGoTop(client,context);
		Document out = p.process(null);
		new SmbcTopChecker().check(out);
		return out;
	}

	@Override
	protected Document login(Document doc) throws IOException,UserProcessException {
		SmbcLogin p = new SmbcLogin(client, context);
		p.setUserId1(userName1);
		p.setUserId2(userName2);
		p.setPassword(password);
		Document out = p.process(doc);
		
		new SmbcLoginChecker().check(out);
		
		return out;
	}

	@Override
	protected Document prepareDownload(Document doc) throws IOException,UserProcessException {
		SmbcGoDetail p = new SmbcGoDetail(client,context,accountNum);
		Document out = p.process(doc);
		return out;
	}

	@Override
	protected File download(Document doc) throws IOException,UserProcessException {
		SmbcDownload p = new SmbcDownload(client,context,outFile);
		File out = p.process(doc);
		return out;
	}

	@Override
	protected void logout(Document doc) throws IOException,UserProcessException {
		SmbcLogout p = new SmbcLogout(client,context);
		p.process(doc);
	}
	
	@SuppressWarnings("static-access")
	@Override
	protected Options getOptions() {
		Options options = new Options();
		options.addOption(new OutFileOption(false));
		options.addOption(OptionBuilder.hasArg(true).withArgName("user1")
				.isRequired(true).withDescription("ユーザー名1")
				.create("user1"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("user2")
				.isRequired(true).withDescription("ユーザー名2")
				.create("user2"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("password")
				.isRequired(true).withDescription("第1暗証")
				.create("password"));
		options.addOption(OptionBuilder.hasArg(true).withArgName("account no")
				.isRequired(true).withDescription("口座番号")
				.create("account"));
		return options;
	}

	@Override
	protected void setParams(CommandLine cmd) {
		userName1 = cmd.getOptionValue("user1");
		userName2 = cmd.getOptionValue("user2");
		password = cmd.getOptionValue("password");
		accountNum = cmd.getOptionValue("account");
		outFile = new File(cmd.getOptionValue("out", DEFAULT_MEISAI_FILENAME));
	}

}
